package com.itstorm.pomeloweather.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.itstorm.pomeloweather.R;
import com.itstorm.pomeloweather.util.AreasCode;

public class FirstAreasActivity extends Activity {

	private ImageButton imgbtnClear;
	private AutoCompleteTextView actvAreas;
	private FrameLayout flResault;
	private TextView tvResault;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first_areas);
		initialization();

	}

	private void initialization() {
		imgbtnClear = (ImageButton) findViewById(R.id.imgbtn_clear);
		actvAreas = (AutoCompleteTextView) findViewById(R.id.actv_areas);
		flResault = (FrameLayout) findViewById(R.id.fl_resault);
		tvResault = (TextView) findViewById(R.id.tv_resault);

		actvAreas.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View arg1, int pos,
					long id) {
				tvResault.setText(actvAreas.getText().toString());
				flResault.setVisibility(View.VISIBLE);
			}
		});
		actvAreas.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (s.toString().length() > 0) {
					imgbtnClear.setVisibility(View.VISIBLE);
				} else {
					imgbtnClear.setVisibility(View.INVISIBLE);
					flResault.setVisibility(View.INVISIBLE);
				}
			}
		});

		flResault.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences weatherList = getSharedPreferences(
						"WeatherList", 0);
				SharedPreferences.Editor editor = weatherList.edit();
				editor.putString("Areas", tvResault.getText().toString().trim());
				editor.commit();

				Intent mIntent = new Intent(FirstAreasActivity.this,
						MainActivity.class);
				startActivity(mIntent);
				overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
				FirstAreasActivity.this.finish();
			}
		});

		imgbtnClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actvAreas.setText("");
			}
		});

		ArrayAdapter<String> av = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line,
				AreasCode.areasArray);
		actvAreas.setAdapter(av);
	}

	// @Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
	}

}
